﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Draws.Controls
{
    public partial class TabAddLineEuro : Base
    {
        protected int Ball1Value
        {
            get { return Int32.Parse(this.txtBall1.Text); }
        }
        protected int Ball2Value
        {
            get { return Int32.Parse(this.txtBall2.Text); }
        }
        protected int Ball3Value
        {
            get { return Int32.Parse(this.txtBall3.Text); }
        }
        protected int Ball4Value
        {
            get { return Int32.Parse(this.txtBall4.Text); }
        }
        protected int Ball5Value
        {
            get { return Int32.Parse(this.txtBall5.Text); }
        }
        protected int LuckyStar1Value
        {
            get { return Int32.Parse(this.txtLuckyStar1.Text); }
        }
        protected int LuckyStar2Value
        {
            get { return Int32.Parse(this.txtLuckyStar2.Text); }
        }

        protected string MillionerRaffleValue
        {
            get { return this.txtRaffle.Text; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (this.DrawLocked)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=2", this.CurrentDraw.id));

            if (this.RemainingFunds <= 0)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=1", this.CurrentDraw.id));

            if (this.RemainingFunds <= 2)
            {
                this.ButtonAddAnotherLine.Visible = false;
            }
        }

        protected void ButtonAddLine_Click(object sender, EventArgs e)
        {

            if (!Page.IsValid)
                return;

            List<int> balls = new List<int>();

            List<int> stars = new List<int>();

            balls.Add(this.Ball1Value);

            if (!balls.Contains(this.Ball2Value))
                balls.Add(this.Ball2Value);
            if (!balls.Contains(this.Ball3Value))
                balls.Add(this.Ball3Value);
            if (!balls.Contains(this.Ball4Value))
                balls.Add(this.Ball4Value);
            if (!balls.Contains(this.Ball5Value))
                balls.Add(this.Ball5Value);

            if (balls.Count != 5)
                return;

            stars.Add(this.LuckyStar1Value);

            if (!stars.Contains(this.LuckyStar2Value))
                stars.Add(this.LuckyStar2Value);

            if (stars.Count != 2)
                return;
            
            balls.Sort();

            stars.Sort();

            bool result = SaveForm(balls,stars,this.MillionerRaffleValue);


            if (result)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=2", this.DrawId));
            else
            {
                FailurePlaceHolder.Visible = true;
            }
        }

        private bool SaveForm(List<int> balls, List<int> stars, string raffle)
        {
            line l = new line();

            l.drawId = this.CurrentDraw.id;
            l.numbers = String.Join(",", balls.ToArray());

            l.numbers += "," + String.Join(",", stars.ToArray());

            l.raffle = raffle;

            if (this.BasePage.BusinessFacade.LineBL.InsertLine(l))
            {
                InsertEventLogEntry("Line [" + l.id.ToString() + "] added.");

                return true;
            }
            else
                return false;



        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            List<int> balls = new List<int>();

            balls.Add(this.Ball1Value);

            if (!balls.Contains(this.Ball2Value))
                balls.Add(this.Ball2Value);
            if (!balls.Contains(this.Ball3Value))
                balls.Add(this.Ball3Value);
            if (!balls.Contains(this.Ball4Value))
                balls.Add(this.Ball4Value);
            if (!balls.Contains(this.Ball5Value))
                balls.Add(this.Ball5Value);
           

            balls.Sort();

            if (balls.Count != 5)
            {
                this.CustomValidator1.ErrorMessage = "Please remove duplicated numbers.";

                args.IsValid = false;
            }

            if (!args.IsValid)
                return;

            foreach (int b in balls)
            {
                if (b >= 51 || b <= 0)
                {
                    this.CustomValidator1.ErrorMessage = "Please enter numbers in range 1-49.";

                    args.IsValid = false;
                    break;
                }
            }

            if (!args.IsValid)
                return;

            List<int> stars = new List<int>();

            stars.Add(this.LuckyStar1Value);

            if (!stars.Contains(this.LuckyStar2Value))
                stars.Add(this.LuckyStar2Value);
           
            stars.Sort();

            if (stars.Count != 2)
            {
                this.CustomValidator1.ErrorMessage = "Please remove duplicated lucky stars.";

                args.IsValid = false;
            }

            if (!args.IsValid)
                return;

            foreach (int b in stars)
            {
                if (b >= 12 || b <= 0)
                {
                    this.CustomValidator1.ErrorMessage = "Please enter lucky stars in range 1-11.";

                    args.IsValid = false;
                    break;
                }
            }

            if (!args.IsValid)
                return;


            string numbers = String.Join(",", balls.ToArray()) + "," + String.Join(",", stars.ToArray());

            int count = this.BasePage.BusinessFacade.LineBL.SelectAllByDrawId(CurrentDraw.id).ToList().Where(d => d.numbers == numbers).Count();

            if (count > 0)
            {
                this.CustomValidator1.ErrorMessage = "Line with selected numbers and stars is already added to this draw.";
                args.IsValid = false;
            }

            string raffle = this.MillionerRaffleValue;

            int countRaffle = this.BasePage.BusinessFacade.LineBL.SelectAllByDrawId(CurrentDraw.id).ToList().Where(d => d.raffle == raffle).Count();

            if (count > 0)
            {
                this.CustomValidator1.ErrorMessage = "Line with entered Raffle code is already added to this draw.";
                args.IsValid = false;
            }

        }

        protected void ButtonAddAnotherLine_Click(object sender, EventArgs e)
        {
            List<int> balls = new List<int>();

            List<int> stars = new List<int>();

            balls.Add(this.Ball1Value);

            if (!balls.Contains(this.Ball2Value))
                balls.Add(this.Ball2Value);
            if (!balls.Contains(this.Ball3Value))
                balls.Add(this.Ball3Value);
            if (!balls.Contains(this.Ball4Value))
                balls.Add(this.Ball4Value);
            if (!balls.Contains(this.Ball5Value))
                balls.Add(this.Ball5Value);

            if (balls.Count != 5)
                return;

            stars.Add(this.LuckyStar1Value);

            if (!stars.Contains(this.LuckyStar2Value))
                stars.Add(this.LuckyStar2Value);

            if (stars.Count != 2)
                return;

            balls.Sort();

            stars.Sort();

            bool result = SaveForm(balls, stars, this.MillionerRaffleValue);

            string numbers = String.Join(",", balls.ToArray()) +", Lucky Stars: "+ String.Join(",", stars.ToArray()) + ", Raffle: " + this.MillionerRaffleValue;

            if (result)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=3&previous={1}", this.DrawId, numbers));
            else
            {
                FailurePlaceHolder.Visible = true;
            }
        }

        protected string PrintPreviousNumber()
        {
            if (Page.Request.QueryString["previous"] != null)
            {
                return String.Format("Last added: {0}", Page.Request.QueryString["previous"].ToString());
            }
            else
                return "";

        }

        protected string PrintFirstFieldId()
        {
            return this.txtBall1.ClientID;

        }

    }
}